package com.grace.controller;

import com.grace.entity.Account;
import com.grace.service.AccountService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 账户web层
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 查询所有账户
     */
    @RequestMapping("/findAll")
    public String findAll(Model model) {
        System.out.println("表现层: findAll方法执行了...");
        //调用service查询
        List<Account> list = accountService.findAll();
        model.addAttribute("list", list);
        return "list";
    }

    /**
     * 保存账户
     */
    @RequestMapping("/saveAccount")
    public void saveAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("表现层: saveAccount方法执行了...");
        //调用service查询
        accountService.saveAccount(account);
//        return "list";
        //假如我想要重定向
        response.sendRedirect(request.getContextPath() + "/account/findAll");
        return;
    }
}
